package com.kk.netty.basice.demo2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Scanner;

@Slf4j
public class CloseFutureClientTest {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup ( );
        ChannelFuture channelFuture = new Bootstrap ( )
                .group (group)
                .channel (NioSocketChannel.class)
                .handler (new ChannelInitializer<Channel> ( ) {
                    @Override
                    protected void initChannel(Channel ch) {
                        ch.pipeline ( ).addLast (new LoggingHandler (LogLevel.DEBUG));
                        ch.pipeline ( ).addLast (new StringEncoder ( ));
                    }
                })
                .connect (new InetSocketAddress ("localhost", 8080));

        Channel channel = channelFuture.sync ( ).channel ( );
        log.debug("{}", channel);
        new Thread (()->{
            Scanner scanner = new Scanner (System.in );
            while (true) {
                String line = scanner.nextLine ( );
                if ("q".equals(line)) {
                    channel.close(); // close 异步操作 1s 之后
//                    log.debug("处理关闭之后的操作"); // 不能在这里善后
                    break;
                }
                channel.writeAndFlush(line);
            }
        },"input").start ();

        // 获取 CloseFuture 对象， 1) 同步处理关闭， 2) 异步处理关闭
        ChannelFuture closeFuture = channel.closeFuture();
        closeFuture.addListener (new ChannelFutureListener ( ) {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                log.debug("处理关闭之后的操作");
                group.shutdownGracefully();
            }
        });
    }
}
